package org.as3search.criteria {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	import org.as3search.config.SearchConfiguration;
	
	/**
	 * <code>GreaterThanOrEqualsExpression</code> evaluates whether a 
	 * property is greater than or equal to a value.
	 * 
	 * @see org.as3search.criteria.EqualityExpression
	 * @see org.as3search.criteria.NotEqualsExpression
	 * @see org.as3search.criteria.GreaterThanExpression
	 * @see org.as3search.criteria.LessThanExpression
	 * @see org.as3search.criteria.LessThanOrEqualsExpression
	 */
	public class GreaterThanOrEqualsExpression extends SimpleExpression {
		/**
		 * Creates a new <code>GreaterThanOrEqualsExpression</code>. 
		 * @param	propertyName	The name of the property to compare to <code>value</code>.
		 * @param	value			The value to compare against the value of <code>propertyName</code>.
		 * @param	configuration	The <code>SearchConfiguration</code> to help in comparison.
		 * @param	ignoreCase		For String comparisons, whether to ignore capitalization.
		 */
		public function GreaterThanOrEqualsExpression(propertyName:String, value:Object, 
				configuration:SearchConfiguration = null, ignoreCase:Boolean=false) {
			super(propertyName, value, configuration, ignoreCase);
		}
		/**
		 * @inheritDoc
		 */
		override public function evaluateProperty(propertyValue:Object):Boolean {
//			make sure there's no null stuff going on.
			if (value == null || propertyValue == null) 
				return false;
//			do string comparison
			if (propertyValue is String) {
				return ignoreCase ? propertyValue.toString().toLowerCase() >= value.toString().toLowerCase() :
					propertyValue.toString() >= value.toString();
			}
//			simple comparison
			if (propertyValue >= value)
				return true;
//			now cover the exceptional case with date objects
			if (propertyValue is Date) {
				if (value is Number || value is int || value is uint) {
					return (propertyValue as Date).time >= Number(value);
				} else if (value is Date) {
					return (propertyValue as Date).time >= (value as Date).time;
				} else {
					return false;
				}
			}
			
			return false;
		}
	}
}